package com.yunc.modules.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.utils.R;
import com.yunc.modules.cms.entity.CmsCategory;
import com.yunc.modules.cms.mapper.CmsCategoryMapper;
import com.yunc.modules.cms.service.ICmsCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 文章分类
 *
 * @author congcong tao
 * @date 2018/6/21 17:11
 */
@Service("cmsCategoryService")
public class CmsCategoryServiceImpl extends ServiceImpl<CmsCategoryMapper, CmsCategory> implements ICmsCategoryService {

    @Autowired
    private CmsCategoryMapper cmsCategoryMapper;

    @Override
    public List<CmsCategory> queryCategoryList(CmsCategory cmsCategory) {
        List<CmsCategory> allList = super.list(null);
        return getCategoryTree(allList, 0L);
    }

    @Override
    public String selectMaxCode(Long parentId) {
        return cmsCategoryMapper.selectMaxCode(parentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveCategory(CmsCategory category) {
        category.setCreateTime(new Date());
        Long parentId = category.getParentId();
        String code = this.selectMaxCode(category.getParentId());
        if (StringUtils.isNotEmpty(code)) {
            int i = Integer.parseInt(code) + 1;
            category.setCode(String.valueOf(i));
        } else {
            // 取出父节点
            if (parentId != null) {
                CmsCategory parentCategory = this.getById(parentId);
                category.setCode(parentCategory.getCode() + "01");
            } else{
                category.setCode("10");
            }
        }
        boolean flag = this.save(category);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R updateCategory(CmsCategory category) {
        CmsCategory currentCategory = this.getById(category.getId());
        Long parentId = category.getParentId();
        if (!parentId.equals(currentCategory.getParentId())) {
            String code = this.selectMaxCode(parentId);
            if (StringUtils.isNotEmpty(code)) {
                int i = Integer.parseInt(code) + 1;
                category.setCode(String.valueOf(i));
            } else {
                // 取出父节点
                if (parentId != null) {
                    CmsCategory parentCategory = this.getById(parentId);
                    category.setCode(parentCategory.getCode() + "01");
                }
            }
        }
        boolean flag = this.updateById(category);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    private List<CmsCategory> getTreeCategory(List<CmsCategory> categoryList) {
        List<CmsCategory> subCategoryList = new ArrayList<>();
        for (CmsCategory cmsCategory : categoryList) {
            QueryWrapper<CmsCategory> ew = new QueryWrapper<CmsCategory>()
                    .eq("parent_id", cmsCategory.getId())
                    .orderByAsc("order_num");
            List<CmsCategory> categoryListC = super.list(ew);
            if (categoryListC != null && categoryListC.size() > 0) {
                cmsCategory.setChildren(getTreeCategory(categoryListC));
            } else {
                cmsCategory.setIsLast(Boolean.TRUE);
            }
            subCategoryList.add(cmsCategory);
        }
        return subCategoryList;
    }

    private List<CmsCategory> getCategoryTree(List<CmsCategory> categoryList, long parentId) {
        List<CmsCategory> result = new ArrayList<>();
        List<CmsCategory> temp = new ArrayList<>();
        for (CmsCategory entity : categoryList) {
            if (entity.getParentId().equals(parentId)) {
                CmsCategory deptScopeMode = new CmsCategory();
                deptScopeMode.setId(entity.getId());
                deptScopeMode.setName(entity.getName());
                deptScopeMode.setParentId(entity.getParentId());
                deptScopeMode.setCode(entity.getCode());
                temp = getCategoryTree(categoryList, entity.getId());
                if (temp.size() > 0) {
                    deptScopeMode.setChildren(temp);
                } else {
                    deptScopeMode.setIsLast(Boolean.TRUE);
                }
                result.add(deptScopeMode);
            }
        }
        return result;
    }
}
